import {Component, OnInit} from '@angular/core';
import {Router} from '@angular/router';
import {BaseComponent} from '../common/base.component';
import {FileUploader} from 'ng2-file-upload';
import {GetEventService} from './getEvent.service';
import {GetEventInfo} from './getEvent';
import {ParamConfig} from '../common/param.config';
import {Http} from '@angular/http';
import 'rxjs/add/operator/toPromise';

declare var $: any;

@Component({
  selector: 'app-getevent',
  templateUrl: './getEvent.component.html'
})
export class GetEventComponent extends BaseComponent implements OnInit {
  uploader: FileUploader = new FileUploader({
    url: ParamConfig.HTTP_WEB_HOST_URL + 'rbFile/upload?type=3',
    method: 'POST',
    itemAlias: 'file',
    removeAfterUpload: true
  });
  uploader1: FileUploader = new FileUploader({
    url: ParamConfig.HTTP_WEB_HOST_URL + 'rbFile/upload?type=1',
    method: 'POST',
    itemAlias: 'file',
    removeAfterUpload: true
  });
  uploader2: FileUploader = new FileUploader({
    url: ParamConfig.HTTP_WEB_HOST_URL + 'rbFile/upload?type=0',
    method: 'POST',
    itemAlias: 'file',
    removeAfterUpload: true
  });
  name: string; // 基点名称
  sort: string; // 排序
  text: string; // 文字
  curPage: Number = 1;
  imgUrl: string;
  fileId: string;
  iconImg: string;
  fileIdTotal = [];
  fileIdVideo: string;
  videoSrc: string;
  fileIdImage: string;
  imageUrl = [];
  curgetEventInfo: GetEventInfo;
  isText: boolean;
  isVideo: boolean;
  isImage: boolean;
  image: boolean;
  video: boolean;
  typeTitle: string;
  systemFileTO = [];
  constructor(private http: Http, private router: Router, private getEventService: GetEventService) {
    super();
  }
  ngOnInit() {
    this.curgetEventInfo = {
      id: '',        // 编号
      eventName: '', // 基点名称
      type: 0,      // 动作类型
      sort: '',      // 排序
      text: '',      // 文字内容
      fileIds: [],   // 文件id
      textId: '',    // 文字对应的id
      fileUrl: '',   // url
      status: '',
      systemFileTO: [{}]     // 文件信息
    };
    this.getNodeIdinfo();
  }

// 获取nodeid
  getNodeIdinfo() {
    this.getEventService.getEventIdInfo()
      .then(res => {
        if (res[0]) {
          this.curgetEventInfo.eventName = res[1].eventName;
          this.curgetEventInfo.sort = res[1].sort;
          this.curgetEventInfo.type = res[1].type;
          console.log(res[1]);
          this.imageUrl = [];
          this.iconImg = res[1].fileUrl;
          this.systemFileTO = res[1].systemFileTO;
          for (const imgUrl of res[1].systemFileTO) {
            this.imageUrl.push(imgUrl.fileUrl);
            this.fileIdTotal.push(imgUrl.fileId);
          }
          if (res[1].systemFileTO.length !== 0) {
            this.curgetEventInfo.text = res[1].systemFileTO[0].text;
          }
          if (this.curgetEventInfo.type === 2) {
            this.isText = false;
            this.isVideo = true;
            this.isImage = false;
            this.image = false;
            this.video = true;
            this.typeTitle = '视频展示';
          }
          if (this.curgetEventInfo.type === 0) {
            this.isText = true;
            this.isVideo = false;
            this.isImage = false;
          }
          if (this.curgetEventInfo.type === 1) {
            this.isText = true;
            this.isVideo = false;
            this.isImage = true;
            this.image = true;
            this.video = false;
            this.typeTitle = '图片展示';
          }
        }
      });
  }

  // 图标
  selectedFileOnChanged(event: any) {
    console.log(event.target.value);
    // 这里是文件选择完成后的操作处理
    this.uploader.queue[0].onSuccess = (response, status, headers) => {
      // 上传文件成功
      if (status === 200) {
        // 上传文件后获取服务器返回的数据
        const tempRes = JSON.parse(response);
        this.fileId = tempRes.systemFileTO.fileId;
      } else {
        // 上传文件后获取服务器返回的数据错误
      }
    };
    this.uploader.queue[0].upload(); // 开始上传
  }

  // 视频
  selectedFileOnChanged1(event: any) {
    console.log(event.target.value);
    // 这里是文件选择完成后的操作处理
    this.uploader1.queue[0].onSuccess = (response, status, headers) => {
      // 上传文件成功
      if (status === 200) {
        // 上传文件后获取服务器返回的数据
        const tempRes = JSON.parse(response);
        this.fileIdVideo = tempRes.systemFileTO.fileId;
        this.videoSrc = tempRes.systemFileTO.fileUrl;
        $('input[type=file]').wrap('<form>').closest('form').get(0).reset();
      } else {
        // 上传文件后获取服务器返回的数据错误
      }
    };
    this.uploader1.queue[0].upload(); // 开始上传
  }

  // 图片
  selectedFileOnChanged2(event: any) {
    // 这里是文件选择完成后的操作处理
    this.uploader2.queue[0].onSuccess = (response, status, headers) => {
      // 上传文件成功
      if (status === 200) {
        // 上传文件后获取服务器返回的数据
        const tempRes = JSON.parse(response);
        this.imgUrl = tempRes.systemFileTO.fileUrl;
        this.fileIdImage = tempRes.systemFileTO.fileId;
      } else {
        // 上传文件后获取服务器返回的数据错误
      }
    };
    this.uploader2.queue[0].upload(); // 开始上传
  }

  // 保存修改
  onEventEdit() {
    if (this.curgetEventInfo.type === 2) {
      this.getEventService.updateLogicVideoinfo(this.curgetEventInfo, this.fileId, this.fileIdVideo)
        .then(res => {
          if (res) {
            this.alert.info('修改成功');
            setTimeout(() => {
              this.router.navigate(['/buz/event']);
            }, 1500);
          } else {
            alert('数据修改失败');
          }
        });
    }
    if (this.curgetEventInfo.type === 0) {
      this.getEventService.updateLogicTextinfo(this.curgetEventInfo, this.fileId)
        .then(res => {
          if (res) {
            this.alert.info('修改成功');
            setTimeout(() => {
              this.router.navigate(['/buz/event']);
            }, 1500);
          } else {
            alert('数据修改失败');
          }
        });
    }
    if (this.curgetEventInfo.type === 1) {
      this.getEventService.updateLogicImageinfo(this.curgetEventInfo, this.fileId, this.fileIdImage)
        .then(res => {
          if (res) {
            this.alert.info('修改成功');
            setTimeout(() => {
              this.router.navigate(['/buz/event']);
            }, 1500);
          } else {
            alert('数据修改失败');
          }
        });
    }
  }

// 删除图片
  onDeleteImage(value) {
    this.getEventService.deleteLogicImageinfo(value)
      .then(res => {
        this.imgUrl = '';
        this.getNodeIdinfo();
      });
  }

  private handleError(error: any): Promise<[boolean, any]> {
    console.error('An error occurred', error); // for demo purposes only
    return Promise.reject([false, error.toString()]);
  }
}
